* ===============================================================
* Date: Dec 13, 2024
* Project: Health and anti-immigration attitudes
*
* Dataset: hlthmigr.dta
* 
*
* This file contains the code to produce descriptive statistics.
* ===============================================================

break
clear all
set more off

* Insert your working directory here:
global PATH "..."
cd $PATH

use data_hlthmigr/hlthmigr.dta, clear
set scheme white_tableau

				
					* Descriptive statistics
					* ======================


* Descriptives table

#delimit 			;
estpost summarize 		
					migrw100 imbgecor imuecltr imwbcntr imsmetn imdfetn impcntr
					health hlthhmp2 medtrunr hltprm2 hlthbhv3 happy
					acetalv2 imblecor lknhlcn imsclbn2
					ppltrstr trstr trstpltr trstprlr trstlglr
					agea gndr2 domicil3tab* mrtsts4tab*
					brncntr2 blgetmg2 migrfam
					eduyrs eduleveltab* fedutab* medutab*
					oesch5tab* hinctntb hincfel2 hincsrc2a
					emprf14tab* emprm14tab* fnsdfml3tab* lknemnytab*
					uempla uemp5yr wrkctratab*
					lrscale nwspol2 clsprty2 polintrr freehmsr
					rlgdgr impsafe ipstrgv imptrad ipeqopt ipudrst ipcrtiv
					[aw=anweight]
					;
#delimit cr

#delimit 	;
estout, 	cells("count(fmt(%9.0fc) label(N)) mean(fmt(2) label(Mean)) sd(fmt(2) label(SD)) min(fmt(2) label(Min)) max(fmt(2) label(Max))") label
			refcat(
			migrw100 "Migration" health "Health" acetalv2 "Moderators and mediators" 
			agea "Demographic factors" domicil3tab1 "Domicile" mrtsts4tab1 "Marital status"
			eduyrs "Socioeconomic factors" eduleveltab1 "Level of education" 
			fedutab1 "Father's education" medutab1 "Mother's education"
			oesch5tab1 "Social class" emprf14tab1 "Father's employment status when respondent 14"
			emprm14tab1 "Mother's employment status when respondent 14"
			fnsdfml3tab1 "Severe financial difficulties in family when growing up"
			lknemnytab1 "How likely not enough money for  household necessities next 12 months"
			wrkctratab1 "Type of work contract"
			lrscale "Political-cultural factors" 
			, nolabel)
			, using output_hlthmigr/descript1.txt, replace
			;
#delimit cr

tab cntryc essround [aweight=anweight]
tabout cntryc essround [aweight=anweight] using output_hlthmigr/descript2.txt, f(0c) replace


					* Bivariate associations
					* ======================

*** Correlation matrix

estpost corr migrw100 imbgecor imuecltr imwbcntr imblecor health acetalv2 ppltrstr trstr trstpltr trstprlr trstlglr, matrix listwise 

esttab using output_hlthmigr/corr.rtf, replace unstack not noobs nostar nonum compress b(2) nonote label

*** Variance components using a 50% subsample to facilitate convergence 
set seed 12345
gen randnum = runiform()
gen subsample = randnum <= 0.50
mixed migr if subsample [pw=anweight] || cntry: , variance
estat icc
drop subsample randnum
 

*** Country level: immigration ~ share of respondents reporting bad or very bad health

tab cntryc hlth2a [aweight=anweight], row nof 

pwcorr c_migrw100_m c_badhlth_pc if atcntry & cntry!="CY" & cntry!="GR" & cntry!="LU", sig
loc r : di %5.3g r(rho) 

#delimit 	;
twoway 		(scatter c_migrw100_m c_badhlth_pc 
			if atcntry & cntry!="CY" & cntry!="GR" & cntry!="LU", 
			jitter(5) mlabel(cntryc) mlabposition(12) mlabsize(small) msize(small)) 
			(lfitci c_migrw100_m c_badhlth_pc 
			if atcntry & cntry!="CY" & cntry!="GR" & cntry!="LU", ciplot(rline) blpattern(dash))
			,  ytitle("Average immigration worry score") xlab(15(5)55) 
			 legend(ring(0) pos(4) label(1 "Countries") size(small) region(col(none)))
			 title("Correlation: r = `r'", size(small) ring(0) pos(3) bcolor(none)) 
			scale(0.9) 
			;
#delimit cr

graph export "output_hlthmigr\cntrycorr.emf", as(emf) replace


*** Individual-level: slopes by country from separate OLS regressions, unadjusted

* get results from separate regressions
statsby inter = _b[_cons] slope = _b[health], by(cntryc) saving(output_hlthmigr/ols, replace): regress migrw100 health [aweight=anweight]
sort cntryc
merge m:1 cntryc using output_hlthmigr/ols
drop _merge
gen yhat_ols = inter + slope * health
separate yhat_ols, by(cntryc)

#delimit 		;
twoway 			connected yhat_ols1-yhat_ols30 health || lfit migrw100 health
				, clwidth(thick) clcolor(black) legend(off) 
				ytitle("Worried about immigration") 
				xlabel(, valuelabel angle(45) grid) scale(0.8) 
				xtitle("Self-rated health")
				;
#delimit cr

graph export "output_hlthmigr\OLS_slopes.png", as(png) replace

sort slope
list slope cntryc if atcntry
sort essround cntryc idno 